package me.zhengjie.modules.web.service.pay.order;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import me.zhengjie.config.exception.WeChatException;
import me.zhengjie.constant.ConstantUtils;
import me.zhengjie.modules.system.domain.UserChatOrder;
import me.zhengjie.modules.system.domain.UserChatPriceSetting;
import me.zhengjie.modules.system.domain.WxUser;
import me.zhengjie.modules.system.service.UserChatOrderService;
import me.zhengjie.modules.system.service.WxUserService;
import me.zhengjie.modules.system.service.dto.UserChatOrderDto;
import me.zhengjie.modules.web.domain.pay.OrderSubmitRequest;
import me.zhengjie.modules.web.domain.pay.OrderSubmitResponse;
import me.zhengjie.modules.web.enums.OrderStatusEnum;
import me.zhengjie.modules.web.service.mybatisplus.IUserChatPriceSettingPlusService;
import me.zhengjie.modules.web.service.order.IOrderService;
import me.zhengjie.modules.web.service.pay.IOrderSubmitService;
import me.zhengjie.modules.weixin.utils.OrderUtil;
import me.zhengjie.request.ATMRContext;
import me.zhengjie.utils.DateUtil;
import me.zhengjie.utils.StringUtils;
import me.zhengjie.utils.enums.OrderPrefixEnum;
import me.zhengjie.utils.enums.OrderTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Objects;

@Service
public class ChatBaiduSubmitServiceImpl implements IOrderSubmitService {

    @Autowired
    private UserChatOrderService chatOrderService;
    @Autowired
    private IOrderService orderService;
    @Autowired
    private IUserChatPriceSettingPlusService chatPriceSettingPlusService;

    @Autowired
    private WxUserService wxUserService;

    @Override
    public int orderType() {
        return OrderTypeEnum.Chat.getOrderType();
    }

    @Override
    public OrderSubmitResponse execute(OrderSubmitRequest request) {
        String chatKey = request.getChatKey();
        if (StringUtils.isBlank(chatKey)){
            throw new WeChatException("缺少参数chatKey");
        }
        LambdaQueryWrapper<UserChatPriceSetting> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserChatPriceSetting::getSettingKey,chatKey);
        wrapper.eq(UserChatPriceSetting::getSettingStatus,ConstantUtils.Valid);
        UserChatPriceSetting one = chatPriceSettingPlusService.getOne(wrapper);
        if (Objects.isNull(one) || ObjectUtil.isEmpty(one.getSettingValue())){
            throw new WeChatException("参数有误");
        }
        String openId = ATMRContext.getContext().getOpenId();
        if (StringUtils.isBlank(openId)){
            openId = request.getBackendOpenId();
        }
        BigDecimal sellPrice = new BigDecimal(one.getSettingValue());
        String orderSn = OrderUtil.GetOrderNumber(OrderPrefixEnum.Chat.getPrefix());
        UserChatOrder userChatOrder = new UserChatOrder();
        userChatOrder.setChatAmount(sellPrice);
        userChatOrder.setOpenId(openId);
        userChatOrder.setOrderNo(orderSn);
        userChatOrder.setCreateTime(DateUtil.TimestampNow());
        userChatOrder.setPayAmount(BigDecimal.ZERO);
        userChatOrder.setPaySerialid("");
        userChatOrder.setPrePayid("");
        userChatOrder.setPayStatus(OrderStatusEnum.WaitingPay.getOrderStatus());
        userChatOrder.setOrderStatus(OrderStatusEnum.WaitingPay.getOrderStatus());
        userChatOrder.setSettingKey(chatKey);
        if (sellPrice.compareTo(BigDecimal.ZERO) == 0 || request.isNotNeedPay()){
            userChatOrder.setPayStatus(OrderStatusEnum.Payed.getOrderStatus());
            userChatOrder.setOrderStatus(OrderStatusEnum.Payed.getOrderStatus());
        }
        WxUser wxUser = wxUserService.findByOpenId(openId);
        userChatOrder.setWxName(wxUser.getNickname());
        userChatOrder.setWxRealName(wxUser.getRealName());
        Timestamp timestamp = Timestamp.valueOf(LocalDateTime.now().plusMinutes(ConstantUtils.DefaultExpireTime));
        userChatOrder.setExpireTime(timestamp);
        UserChatOrderDto examOrderDto = chatOrderService.create(userChatOrder);
        OrderSubmitResponse response = new OrderSubmitResponse();
        response.setOrderId(examOrderDto.getOrderId());
        response.setOrderSn(orderSn);
        response.setOrderStatus(userChatOrder.getOrderStatus());

        orderService.orderExpire2Redis(orderType(),orderSn,timestamp);
        return response;
    }

}
